﻿local _, ns = ...

----------------------------------
--禁用CVar设置
function ns.forbidSetCVars()
	for groupName, groupFrames in pairs(ns.CVarFrames) do
		if groupName == "TextFrames" then
			for _, frame in ipairs(groupFrames) do
				if PlateColorDB.disableCVarSet then
					frame:SetTextColor(0.5, 0.5, 0.5)
				else
					frame:SetTextColor(0,1,0)
				end
			end
		end
		if groupName == "Frames" then
			for _, frame in ipairs(groupFrames) do
				if PlateColorDB.disableCVarSet then
					frame:Disable()
				else
					frame:Enable()
				end
			end
		end
	end
end
--重置cvar
function ns.ReSetCVar()
	ns.forbidSetCVars()
	SetCVar("nameplateGlobalScale",select(2,C_CVar.GetCVarInfo("nameplateGlobalScale")))
	SetCVar("nameplateSelectedScale",select(2,C_CVar.GetCVarInfo("nameplateSelectedScale")))
	SetCVar("nameplateLargerScale",select(2,C_CVar.GetCVarInfo("nameplateLargerScale")))
	SetCVar("nameplateOverlapV",select(2,C_CVar.GetCVarInfo("nameplateOverlapV")))
	SetCVar("nameplateOverlapH",select(2,C_CVar.GetCVarInfo("nameplateOverlapH")))
	SetCVar("nameplateOccludedAlphaMult",select(2,C_CVar.GetCVarInfo("nameplateOccludedAlphaMult")))
	SetCVar("nameplateMaxAlpha",select(2,C_CVar.GetCVarInfo("nameplateMaxAlpha")))
	SetCVar("nameplateMinAlpha",select(2,C_CVar.GetCVarInfo("nameplateMinAlpha")))
	SetCVar("NameplatePersonalShowAlways",select(2,C_CVar.GetCVarInfo("NameplatePersonalShowAlways")))
	SetCVar("nameplateSelfScale",select(2,C_CVar.GetCVarInfo("nameplateSelfScale")))

	SetCVar("NamePlateHorizontalScale",select(2,C_CVar.GetCVarInfo("NamePlateHorizontalScale")))	--宽度
	SetCVar("NamePlateVerticalScale",select(2,C_CVar.GetCVarInfo("NamePlateVerticalScale")))	--高度
	SetCVar("nameplateShowFriendlyNPCs",select(2,C_CVar.GetCVarInfo("nameplateShowFriendlyNPCs")))	--友方npc

	SetCVar("namePlateMinScale",select(2,C_CVar.GetCVarInfo("namePlateMinScale")))	--距离缩放
	SetCVar("namePlateMaxScale",select(2,C_CVar.GetCVarInfo("namePlateMaxScale")))	--距离缩放

	--姓名版始终保持在屏幕内
	SetCVar("nameplateOtherTopInset",select(2,C_CVar.GetCVarInfo("nameplateOtherTopInset")))
	SetCVar("nameplateOtherBottomInset",select(2,C_CVar.GetCVarInfo("nameplateOtherBottomInset")))
	SetCVar("nameplateLargeTopInset",select(2,C_CVar.GetCVarInfo("nameplateLargeTopInset")))
	SetCVar("nameplateLargeBottomInset",select(2,C_CVar.GetCVarInfo("nameplateLargeBottomInset")))
	SetCVar("nameplateShowDebuffsOnFriendly",select(2,C_CVar.GetCVarInfo("nameplateShowDebuffsOnFriendly")))--不显示友方debuff
	SetCVar("nameplateShowOnlyNames",select(2,C_CVar.GetCVarInfo("nameplateShowOnlyNames")))		--开启仅显名字模式
end


--初始化
ns.event("PLAYER_ENTERING_WORLD", function()
	if PlateColorDB.disableCVarSet then ns.forbidSetCVars() return end
	SetCVar("nameplateGlobalScale", PlateColorDB.npScale)
	SetCVar("nameplateLargerScale", 1.1)	--精英
	SetCVar("nameplateSelectedScale", PlateColorDB.npTargetScale)
	SetCVar("nameplateOverlapV", PlateColorDB.npOverlapV)
	SetCVar("nameplateOverlapH", PlateColorDB.npOverlapH)
	SetCVar("nameplateOccludedAlphaMult", PlateColorDB.npOccludedAlpha)
	SetCVar("nameplateMaxAlpha", PlateColorDB.npNoTargetAlpha)
	SetCVar("nameplateMinAlpha", PlateColorDB.npNoTargetAlpha)
	SetCVar("NameplatePersonalShowAlways",PlateColorDB.selfshown and 1 or 0)	--始终显示个人资源
	SetCVar("nameplateSelfScale", PlateColorDB.selfMpScale)
	SetCVar("NamePlateHorizontalScale", PlateColorDB.npWidht)	--宽度
	SetCVar("NamePlateVerticalScale", PlateColorDB.npHeight)	--高度
	SetCVar("nameplateShowFriendlyNPCs",1)	--友方npc
	SetCVar("namePlateMinScale", 1)	--距离缩放
	SetCVar("namePlateMaxScale", 1)	--距离缩放
	SetCVar("ShowClassColorInNameplate", 1)	--职业颜色
	SetCVar('ShowClassColorInFriendlyNameplate', 1)--友方职业颜色
	SetCVar("nameplateMaxDistance", PlateColorDB.npRange)	--血条显示范围
	SetCVar("nameplateOtherAtBase", 0)	--血条位置，预设0头上，1头上但离怪近，2脚下，
	--姓名版始终保持在屏幕内
	SetCVar("nameplateOtherTopInset", 0.08)
	SetCVar("nameplateOtherBottomInset", 0.1)
	SetCVar("nameplateLargeTopInset", 0.08)
	SetCVar("nameplateLargeBottomInset", 0.1)
	
	C_NamePlate.SetNamePlateFriendlySize(PlateColorDB.friendnpX,PlateColorDB.friendnpY)--设置一次
	if PlateColorDB.OnlyName then --仅显示友方
		SetCVar("nameplateShowDebuffsOnFriendly", 0)--不显示友方debuff
		SetCVar("nameplateShowOnlyNames", 1)		--开启仅显名字模式
		
	else
		SetCVar("nameplateShowOnlyNames", 0)--关闭仅显名字模式
	end
	C_Timer.After(1,function()--如果没进战斗延迟再设置一次友方
		if not InCombatLockdown() then
			C_NamePlate.SetNamePlateFriendlySize(PlateColorDB.friendnpX,PlateColorDB.friendnpY)--友方血条上下间距缩小
		end
	end)
end)
ns.event("CVAR_UPDATE", function(cvar,value)
	--友方点击穿透https://nga.178.com/read.php?tid=40193442&_fp=2
	--隐藏友方血条时设置点击穿透
	if cvar ~= "nameplateShowOnlyNames" then return end 
	if value == "1" or value == 1 then
		C_NamePlate.SetNamePlateFriendlyClickThrough(true)
	else
		C_NamePlate.SetNamePlateFriendlyClickThrough(false)
	end
end)

local function AddBlizFrame(unitFrame)
	if not unitFrame.namePlateUnitToken then
		unitFrame.namePlateUnitToken = unitFrame.unit
	end
end
hooksecurefunc("CompactUnitFrame_UpdateWidgetsOnlyMode", function(frame)
	if GetCVar("nameplateShowOnlyNames") == "0" then return end
	if not frame then return end
	if frame:IsForbidden() then return end
	if UnitIsUnit(frame.unit,"player") then return end
	if not string.match(frame.unit,"nameplate") then return end
	CompactUnitFrame_SetHideHealth(frame, not UnitCanAttack("player",frame.unit), 1)
end)

--血条材质
function ns.UpdateHpTexture(unitFrame)
	if not unitFrame or not unitFrame.unit then return end

	local targetIsYou = UnitIsUnit(unitFrame.unit.."target", "player")

	if (PlateColorDB.targetIsyou == true  and (UnitCastingInfo(unitFrame.unit) ~= nil or UnitChannelInfo(unitFrame.unit) ~= nil) and targetIsYou == true) then
		unitFrame.healthBar:SetStatusBarTexture("Interface\\Addons\\PlateColor\\texture\\HP-focus")
	else
		if PlateColorDB.focusTexture and UnitIsUnit(unitFrame.unit,"focus") then
			unitFrame.healthBar:SetStatusBarTexture("Interface\\Addons\\PlateColor\\texture\\HP-focus")
		elseif unitFrame.useBarTexture and unitFrame.useBarTexture.spellId and type(PlateColorDB["Auras"][unitFrame.useBarTexture.spellId]) == "table" and
				PlateColorDB["Auras"][unitFrame.useBarTexture.spellId]["usetexture"] and
				(unitFrame.useBarTexture.applications >= PlateColorDB["Auras"][unitFrame.useBarTexture.spellId]["stack"] or unitFrame.useBarTexture.applications == 0) then--光环变色
			unitFrame.healthBar:SetStatusBarTexture("Interface\\Addons\\PlateColor\\texture\\HP-grid")
		elseif ns.HpTextures[PlateColorDB.hpTexture] then
			unitFrame.healthBar:SetStatusBarTexture(ns.HpTextures[PlateColorDB.hpTexture])
		else
			unitFrame.healthBar:SetStatusBarTexture(ns.HpTextures["PlateColor"])
		end
	end
end

--设置斩杀分割线
local function barkillper(unitFrame)
	if not PlateColorDB.lessHpLine then return end
	if not unitFrame.lowHpline then
		local height = unitFrame.healthBar:GetHeight()
		unitFrame.lowHpline = unitFrame.healthBar:CreateTexture(nil, "OVERLAY")
		unitFrame.lowHpline:SetSize(2, height)	
		unitFrame.lowHpline:SetTexture("Interface\\Buttons\\WHITE8x8")
	end
	if unitFrame.lowHpline then
		local width = unitFrame.healthBar:GetWidth()
		local linepoint = ns.killvalue
		unitFrame.lowHpline:SetPoint("LEFT",unitFrame.healthBar,"LEFT",linepoint*width/100, 0)
		unitFrame.lowHpline:SetShown(linepoint ~= 0 and unitFrame.unit and UnitCanAttack("player",unitFrame.unit))
	end
end

--放大缩小隐藏
local function platenamecolor(unitFrame)
	if unitFrame:IsForbidden() then return end
	if not unitFrame.unit then return end
	if not string.match(unitFrame.unit,"nameplate") then return end

	local guid = UnitGUID(unitFrame.unit)
	local _, _, _, _, _, id = strsplit("-", guid or "") 

	unitFrame:Show()
	unitFrame:SetScale(1)
	unitFrame:SetPoint("TOPLEFT",0,0)
	unitFrame:SetPoint("BOTTOMRIGHT",0,0)
	--宠物缩小
	if PlateColorDB.petScale and ns.GetUnitIsPet(unitFrame.unit) then
		unitFrame:SetScale(0.5)
		unitFrame:SetPoint("TOPLEFT",80,-50)
		unitFrame:SetPoint("BOTTOMRIGHT",-80,50)
	end
	--NPC的放大缩小
	if type(PlateColorDB["NPCs"][id]) == "table" then
		if PlateColorDB["NPCs"][id]["usehide"] and not UnitIsUnit(unitFrame.displayedUnit,"target") then
			unitFrame:Hide()
		else
			unitFrame:Show()
		end
		if PlateColorDB["NPCs"][id]["usescale"] then
			unitFrame:SetScale(0.5)
			unitFrame:SetPoint("TOPLEFT",70,-50)
			unitFrame:SetPoint("BOTTOMRIGHT",-70,50)
		else
			unitFrame:SetScale(1)
			unitFrame:SetPoint("TOPLEFT",0,0)
			unitFrame:SetPoint("BOTTOMRIGHT",0,0)
		end
	end

end

--职业图标
local function CteatClassIcon(unitFrame)
	if not PlateColorDB.classIcon then return end
	if not unitFrame then return end
	if not unitFrame.PCClassIcon then
		unitFrame.PCClassIcon = unitFrame:CreateTexture(nil, "BACKGROUND")
		unitFrame.PCClassIcon:SetSize(30,30)
		unitFrame.PCClassIcon:Hide()
		unitFrame.PCClassIconmask = unitFrame:CreateMaskTexture(nil, "BACKGROUND")
		unitFrame.PCClassIconmask:SetAllPoints(unitFrame.PCClassIcon)
		unitFrame.PCClassIconmask:SetTexture("Interface/CHARACTERFRAME/TempPortraitAlphaMask", "CLAMPTOBLAunitFrameADDITIVE", "CLAMPTOBLAunitFrameADDITIVE")
		unitFrame.PCClassIcon:AddMaskTexture(unitFrame.PCClassIconmask)
	end
	if not unitFrame.unit then return end
	if not unitFrame.unit or not UnitIsPlayer(unitFrame.unit) or UnitIsUnit(unitFrame.unit,"player") then
		unitFrame.PCClassIcon:Hide()
		return
	end
	if unitFrame.healthBar:IsShown() and C_NamePlate.GetNamePlateFriendlySize()>50 then
		unitFrame.PCClassIcon:SetPoint("RIGHT",unitFrame.healthBar,"LEFT",-2,0)
	elseif unitFrame.nametext and unitFrame.nametext:IsShown() then
		unitFrame.PCClassIcon:SetPoint("RIGHT",unitFrame.nametext,"LEFT",-2,0)
	elseif unitFrame.name and unitFrame.name:IsShown() then
		unitFrame.PCClassIcon:SetPoint("RIGHT",unitFrame.name,"LEFT",-2,0)
	else
		unitFrame.PCClassIcon:Hide()
	end
	
	local _, class = UnitClass(unitFrame.unit);
	if ( class ) then
		local classIconAtlas = GetClassAtlas(class);
		if ( classIconAtlas ) then
			unitFrame.PCClassIcon:SetAtlas(classIconAtlas);
			unitFrame.PCClassIcon:Show()
		end
	else
		unitFrame.PCClassIcon:Hide()
	end
end

--等级文本
local function CteatLevelText(unitFrame)
	if not PlateColorDB.levelText then return end
	if not unitFrame then return end
	if not unitFrame.LevelText then
		unitFrame.LevelText = unitFrame:CreateFontString(nil, "ARTWORK")
		unitFrame.LevelText:SetFont(ns.fonts, 16, "OUTLINE")
	end
	unitFrame.LevelText:Hide()
	if not unitFrame.unit then return end
	--if not UnitCanAttack("player", unitFrame.unit) then return end
	if not unitFrame.healthBar:IsShown() then return end
	local NpLevel = UnitLevel(unitFrame.unit)
	local PlayerLevel = UnitLevel("player")
	if NpLevel < 0 then return end
	if NpLevel >= PlayerLevel and NpLevel <= PlayerLevel + 2 then return end
	local difficulty = C_PlayerInfo.GetContentDifficultyCreatureForPlayer(unitFrame.unit)
	local color = GetDifficultyColor(difficulty);
	unitFrame.LevelText:SetVertexColor(color.r, color.g, color.b);
	unitFrame.LevelText:SetText(NpLevel)
	if unitFrame.PCClassIcon and unitFrame.PCClassIcon:IsShown() then
		unitFrame.LevelText:SetPoint("RIGHT",unitFrame.healthBar,"LEFT",-32,0)
	elseif unitFrame.ClassificationFrame.classificationIndicator:IsShown() and not PlateColorDB.hideFlags then
		unitFrame.LevelText:SetPoint("RIGHT",unitFrame.healthBar,"LEFT",-12,0)
	else
		unitFrame.LevelText:SetPoint("RIGHT",unitFrame.healthBar,"LEFT",-1,0)
	end
	unitFrame.LevelText:Show()
end

--调用
local namepoint = CreateFrame("Frame")
namepoint:RegisterEvent("NAME_PLATE_UNIT_ADDED")
namepoint:SetScript("OnEvent", function(self, event,frame)
	local namePlate = C_NamePlate.GetNamePlateForUnit(frame,false)
	local unitFrame = namePlate.UnitFrame
	AddBlizFrame(unitFrame)
	barkillper(unitFrame)
	platenamecolor(unitFrame)
	CteatClassIcon(unitFrame)
	CteatLevelText(unitFrame)
	ns.UpdateHpTexture(unitFrame)
end)
hooksecurefunc("CompactUnitFrame_UpdateName", platenamecolor)

--设置焦点材质
local SetFocusEvent = CreateFrame('Frame')
SetFocusEvent:RegisterEvent("PLAYER_FOCUS_CHANGED")
SetFocusEvent:SetScript("OnEvent", function(self, event,frame)
	if not PlateColorDB.focusTexture then return end
	for i, namePlate in ipairs(C_NamePlate.GetNamePlates()) do
		ns.UpdateHpTexture(namePlate.UnitFrame)
	end
end)

--血条右侧显示箭头
function ns.RightArrow(frame)
	if frame:IsForbidden() then return end
	if not frame or not frame.unit then return end
	if string.match(frame.unit,"nameplate") then
		if not frame.arrow then
			frame.arrow = frame.healthBar:CreateTexture(nil, "ARTWORK")
			frame.arrow:SetTexture("Interface\\Addons\\PlateColor\\texture\\blue.tga")
			frame.arrow:SetSize(45, 45)
			frame.arrow:Hide()
		end
		if not frame.arrow then return end
		
		frame.arrow:SetPoint("LEFT", frame.healthBar, "RIGHT", -2, 0)
		if not PlateColorDB.hpAuraToLeft then
			for i = ns.RightAuraMax,1,-1 do
				if frame["RightAuras"..i] and frame["RightAuras"..i]:IsShown() then
					frame.arrow:SetPoint("LEFT", frame.healthBar, "RIGHT", i * (ns.RightAuraSize+ns.RightAuraSize/15)-2, 0)
					break
				end
			end
		end
		local needshow = UnitIsUnit(frame.displayedUnit, "target") and UnitCanAttack("player",frame.unit)
		if PlateColorDB.arrow and not UnitIsUnit("player", frame.unit) and needshow then
			frame.arrow:Show()
		else
			frame.arrow:Hide()
		end
	end
end
hooksecurefunc("CompactUnitFrame_UpdateHealthBorder", ns.RightArrow)

--施法目标
local casttarget = CreateFrame("Frame")
casttarget:RegisterEvent("NAME_PLATE_UNIT_ADDED")
casttarget:RegisterEvent("UNIT_TARGET")
casttarget:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START")
casttarget:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP")
casttarget:RegisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE")
casttarget:RegisterEvent("UNIT_SPELLCAST_DELAYED")
casttarget:RegisterEvent("UNIT_SPELLCAST_FAILED")
casttarget:RegisterEvent("UNIT_SPELLCAST_FAILED_QUIET")
casttarget:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED")
casttarget:RegisterEvent("UNIT_SPELLCAST_INTERRUPTIBLE")
casttarget:RegisterEvent("UNIT_SPELLCAST_NOT_INTERRUPTIBLE")
casttarget:RegisterEvent("UNIT_SPELLCAST_START")
casttarget:RegisterEvent("UNIT_SPELLCAST_STOP")
casttarget:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
casttarget:SetScript("OnEvent", function(self, event)
	for i, namePlate in ipairs(C_NamePlate.GetNamePlates()) do
		local frame = namePlate.UnitFrame
		if frame then
		if not frame.unit then return end
		local namea = UnitName(frame.unit.."target")
		local myname = UnitName("Player")
		local players = UnitIsPlayer(frame.unit)
		local _, class = UnitClass(frame.unit.."target")
		local guid = UnitGUID(frame.unit)
		local _, _, _, _, _, id = strsplit("-", guid or "") 
		local color = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[class] or RAID_CLASS_COLORS[class]
			if not frame.ta then
			frame.ta = frame.healthBar:CreateFontString(nil, "ARTWORK")
			frame.ta:SetFont(ns.fonts, PlateColorDB.castTargetSize, 'OUTLINE')
			frame.ta:SetPoint("BOTTOMLEFT",frame.healthBar,"TOPRIGHT",-10,0)
			frame.ta:Hide()
		end
			ns.UpdateHpTexture(frame)
		if PlateColorDB.castTarget == true and players == false then
			if ( UnitCastingInfo(frame.unit) ~= nil or UnitChannelInfo(frame.unit) ~= nil ) or ns.ShowTarget[id]  then	--and frame.ta:GetText() == myname then
				if color then frame.ta:SetTextColor(color.r, color.g, color.b) end
				frame.ta:SetText(namea)
				frame.ta:Show()
			else
				frame.ta:Hide()
			end
		elseif PlateColorDB.targetIsyou == true and players == true and UnitName(frame.unit) ~= myname then
			if color then frame.ta:SetTextColor(color.r, color.g, color.b) end
			frame.ta:SetText(namea)
			if frame.ta:GetText() == myname then
				frame.ta:Show()
			else
				frame.ta:Hide()
			end
		else 
			frame.ta:Hide()
		end
		end
		if UnitIsUnit("player", frame.unit) and frame.ta then
			frame.ta:Hide()
		end
	end
end)

--施法条计时
hooksecurefunc(CastingBarMixin,"OnUpdate", function(self,elapsed)
	if PlateColorDB.castTime == false then return end
	if not self then return end
	if self:IsForbidden() then return end
	local frame = self:GetParent()
	if not string.match(self.unit,"nameplate") then return end
	if not frame.castCooldown and frame.castBar then
		frame.castCooldown = frame.castBar:CreateFontString(nil)
		frame.castCooldown:SetFont(SystemFont_Outline_Small:GetFont(), 13, "OUTLINE")	--施法时间文字大小
		frame.castCooldown:SetWidth(100)
		frame.castCooldown:SetJustifyH("RIGHT")
		frame.castCooldown:SetPoint("RIGHT", frame.castBar, "RIGHT", 0, 0)
	end
	if not frame.castCooldown then return end
	if (self.maxValue-self.value)<0 then
		frame.castCooldown:SetText(" ")
	elseif UnitCastingInfo(frame.unit) then
		frame.castCooldown:SetText(format("%.1f",self.maxValue-self.value,0))
	elseif UnitChannelInfo(frame.unit) then
		frame.castCooldown:SetText(format("%.1f",self.value,0))
	end
	frame.castCooldown:SetShown(UnitCanAttack("player",self:GetParent().unit))
end)

--施法条高度
hooksecurefunc(CastingBarMixin,"OnShow", function(self,elapsed)
	if not self then return end
	if self:IsForbidden() then return end
	if not string.match(self.unit,"nameplate") then return end
	self:SetHeight(PlateColorDB.castBarHeight)
	if self.Text then
		local fonts,scale = self.Text:GetFont()
		self.Text:SetFont(fonts,scale,"OUTLINE")
	end
	if not PlateColorDB.castold then return end
	self.Spark:SetHeight(self:GetHeight()+10)
	self:SetStatusBarTexture(ns.HpTextures[PlateColorDB.CastBarTexture])
	self.Background:SetTexture(130937)
	self.Background:SetVertexColor(0.1,0.1,0.1,0.9)
	if select(8,UnitCastingInfo(self.unit)) or select(7,UnitChannelInfo(self.unit)) then 
		self:SetStatusBarColor(0.6,0.6,0.6,1)
	elseif UnitCastingInfo(self.unit) then
		self:SetStatusBarColor(0.9,0.9,0,1)
	elseif UnitChannelInfo(self.unit) then
		self:SetStatusBarColor(0,1,0,1)
	end
end)

--施法条事件变更颜色和设置相关
hooksecurefunc(CastingBarMixin,"OnEvent",function(self,event,units)
	if not PlateColorDB.castold then return end
	if self:IsForbidden() then return end
	if not self.unit then return end
	if self.unit ~= units then return end;
	if not string.match(self.unit,"nameplate") then return end
	self.Spark:SetHeight(self:GetHeight()+10)
	self:SetStatusBarTexture(ns.HpTextures[PlateColorDB.CastBarTexture])
	self.Background:SetTexture(130937)
	self.Background:SetVertexColor(0.1,0.1,0.1,0.9)
	if event=="UNIT_SPELLCAST_EMPOWER_START" then
		self:SetStatusBarColor(0,0,0,0)
		if not self.ChargeTier1 then return end
		self.ChargeTier1.Normal:SetTexture("Interface\\Addons\\PlateColor\\texture\\WHITE8x8")
		self.ChargeTier1.Normal:SetVertexColor(1, 0, 0 ,1)
		self.ChargeTier1.Disabled:SetTexture("Interface\\Addons\\PlateColor\\texture\\WHITE8x8")
		self.ChargeTier1.Disabled:SetVertexColor(1, 0, 0, 0.2)
		if not self.ChargeTier2 then return end
		self.ChargeTier2.Normal:SetTexture("Interface\\Addons\\PlateColor\\texture\\WHITE8x8")
		self.ChargeTier2.Normal:SetVertexColor(0, 1, 0 ,1)
		self.ChargeTier2.Disabled:SetTexture("Interface\\Addons\\PlateColor\\texture\\WHITE8x8")
		self.ChargeTier2.Disabled:SetVertexColor(0, 1, 0, 0.2)
		if not self.ChargeTier3 then return end
		self.ChargeTier3.Normal:SetTexture("Interface\\Addons\\PlateColor\\texture\\WHITE8x8")
		self.ChargeTier3.Normal:SetVertexColor(1, 1, 0 ,1)
		self.ChargeTier3.Disabled:SetTexture("Interface\\Addons\\PlateColor\\texture\\WHITE8x8")
		self.ChargeTier3.Disabled:SetVertexColor(1, 1, 0, 0.2)
	elseif event=="UNIT_SPELLCAST_FAILED" or event=="UNIT_SPELLCAST_INTERRUPTED" then
		self:SetStatusBarColor(1,0,0,1)
	elseif select(8,UnitCastingInfo(self.unit)) or select(7,UnitChannelInfo(self.unit)) then 
		self:SetStatusBarColor(0.6,0.6,0.6,1)
	elseif UnitCastingInfo(self.unit) then
		self:SetStatusBarColor(0.9,0.9,0,1)
	elseif UnitChannelInfo(self.unit) then
		self:SetStatusBarColor(0,1,0,1)
	end
end)
--完成施法材质,因为有施法完成动画所以也要加一个
hooksecurefunc(CastingBarMixin,"FinishSpell",function(self)
	if not PlateColorDB.castold then return end
	if self:IsForbidden() then return end
	if not self.unit then return end
	if not string.match(self.unit,"nameplate") then return end
	self:SetStatusBarTexture(ns.HpTextures[PlateColorDB.CastBarTexture])
end) 

--吸收盾
function ns.absUpdatePoint(unitFrame)
	if not PlateColorDB.absorbText then return end
	if not unitFrame.abs then return end
	local offsetX = (unitFrame.LevelText and unitFrame.LevelText:IsShown()) and -20 or 0
	unitFrame.abs:SetPoint("RIGHT", unitFrame.healthBar, "LEFT",offsetX +-3,0)
	if PlateColorDB.hpAuraToLeft then
		for i = ns.RightAuraMax,1,-1 do
			if unitFrame["RightAuras"..i] and unitFrame["RightAuras"..i]:IsShown() then
				unitFrame.abs:SetPoint("RIGHT", unitFrame.healthBar, "LEFT", -1*(i * (ns.RightAuraSize+ns.RightAuraSize/15))-3, 0)
				break
			end
		end
	end
end
local plateabs = CreateFrame("Frame")
plateabs:RegisterEvent("UNIT_ABSORB_AMOUNT_CHANGED")
plateabs:RegisterEvent("NAME_PLATE_UNIT_ADDED")
plateabs:SetScript("OnEvent", function(self, event,frame)
	if not PlateColorDB.absorbText then return end
	if not string.match(frame,"nameplate") then return end
	local namePlate = C_NamePlate.GetNamePlateForUnit(frame,false)
	if not namePlate then return end
	local unitFrame = namePlate.UnitFrame
	if not unitFrame then return end
	if not unitFrame.unit then return end
	if not unitFrame.abs then
		unitFrame.abs = unitFrame.healthBar:CreateFontString(frame.."absorb", "OVERLAY")
		unitFrame.abs:SetFont(ns.fonts, 21, "OUTLINE")
	end
	if unitFrame.abs then
		unitFrame.abs:SetText(ns.value(UnitGetTotalAbsorbs(unitFrame.unit),PlateColorDB.hpUnits))
		unitFrame.abs:SetShown(UnitCanAttack("player",unitFrame.unit))
	end
	ns.absUpdatePoint(unitFrame)
end)

--被打断在姓名版施法条显示是谁打断
local interrupt = CreateFrame("Frame", nil, UIParent)
interrupt:RegisterEvent('COMBAT_LOG_EVENT_UNFILTERED')
interrupt:SetScript("OnEvent", function(self, event, unit)
	if PlateColorDB.castIntName == false then return end
	local _, subevent, _, sourceGUID, sourceName, sourceFlag, _, destGUID = CombatLogGetCurrentEventInfo()
	if (subevent ~= 'SPELL_INTERRUPT') then return end
	if (not sourceName) then return end
	local onlyname,_ = strsplit("-",sourceName) --Ambiguate(sourceName,"short")
	for i, namePlate in ipairs(C_NamePlate.GetNamePlates()) do
	local unitFrame = namePlate.UnitFrame
	if UnitGUID(unitFrame.unit) == destGUID then
		local _,class = GetPlayerInfoByGUID(""..sourceGUID)
		local color
		if class then
			color = RAID_CLASS_COLORS[class].colorStr
		else
			color = "ffFFFFFF"
		end
		if not unitFrame.castBar then return end
		if not unitFrame.castBar.Text:GetText() then return end
		unitFrame.castBar.Text:SetText(unitFrame.castBar.Text:GetText().."- ".."|c"..color..onlyname.."|r")
	end
	end
end)

--生命值
local hp = function(unitFrame)
	if unitFrame:IsForbidden() then return end
	if not string.match(unitFrame.unit,"nameplate") then return end
	if not unitFrame.hp then
		unitFrame.hp = unitFrame.healthBar:CreateFontString(nil, "OVERLAY")
		unitFrame.hp:SetFont(ns.fonts, 14, 'OUTLINE')
	end
	if not unitFrame.unit then return end
	--位置
	if PlateColorDB.hpPoint == 1 then
		unitFrame.hp:SetPoint("LEFT",0,0)
	elseif PlateColorDB.hpPoint == 2 then 
		unitFrame.hp:SetPoint("CENTER",5,0)
	elseif PlateColorDB.hpPoint == 3 then
		unitFrame.hp:SetPoint("RIGHT",0,0)
	end
	unitFrame.hp:SetScale(PlateColorDB.hpScale)
	--数值方式
	local CurHealth = UnitHealth(unitFrame.unit)
	local MaxHealth = UnitHealthMax(unitFrame.unit)
	if PlateColorDB.hpPercent and PlateColorDB.hpValue then
		unitFrame.hp:SetText(ns.value(CurHealth,PlateColorDB.hpUnits)..PlateColorDB.Delimiter..ns.percent(CurHealth,MaxHealth))
	elseif PlateColorDB.hpPercent then
		unitFrame.hp:SetText(ns.percent(CurHealth,MaxHealth))
	elseif PlateColorDB.hpValue then
		unitFrame.hp:SetText(ns.value(CurHealth,PlateColorDB.hpUnits))
	end

	if not UnitIsUnit(unitFrame.unit,"Player") and UnitCanAttack("player",unitFrame.unit) then
		unitFrame.hp:Show()
	else
		unitFrame.hp:Hide()
	end
end
hooksecurefunc("CompactUnitFrame_UpdateMaxHealth", hp)
hooksecurefunc("CompactUnitFrame_UpdateHealth", hp)

--个人资源材质
hooksecurefunc(NamePlateDriverFrame.classNamePlatePowerBar,"SetupBar",function(bar)
	if PlateColorDB.hpTexture == "noHpTexture" then
		return
	elseif ns.HpTextures[PlateColorDB.hpTexture] then 
		bar:SetStatusBarTexture(ns.HpTextures[PlateColorDB.hpTexture])
	else
		bar:SetStatusBarTexture(ns.HpTextures["PlateColor"])
	end
end)

--个人资源数值
hooksecurefunc(NamePlateDriverFrame.classNamePlatePowerBar,"UpdatePower",function(bar)
	if bar and not bar.PowerText then
		bar.PowerText = bar:CreateFontString("selfpowertext", "ARTWORK")
		bar.PowerText:SetPoint("TOP",0,0)
	end
	if bar.PowerText then
		bar.PowerText:SetShown(PlateColorDB.selfMpText and bar.FullPowerFrame.maxValue < 1000)
		bar.PowerText:SetFont(ns.fonts, 13*PlateColorDB.selfMpTextScale, 'OUTLINE')
		bar.PowerText:SetText(bar.currValue)
	end
end)
local selfManaUpada = CreateFrame("Frame")
selfManaUpada:RegisterEvent("UNIT_POWER_FREQUENT")
selfManaUpada:SetScript("OnEvent", function(self, event,unit,PowerType)
	if not selfpowertext then return end
	if not string.match(unit,"nameplate")  then return end
	if not UnitIsUnit(unit,"player") then return end
	selfpowertext:SetText(UnitPower(unit))
end)

--隐藏精英标志
local HideFlags = CreateFrame("Frame")
HideFlags:RegisterEvent("NAME_PLATE_UNIT_ADDED")
HideFlags:SetScript("OnEvent", function(self, event,frame)
	if not string.match(frame,"nameplate")  then return end
	local namePlate = C_NamePlate.GetNamePlateForUnit(frame,false)
	if not namePlate then return end
	local unitFrame = namePlate.UnitFrame
	unitFrame.ClassificationFrame:SetFrameStrata("BACKGROUND")
	if not PlateColorDB.hideFlags then return end
	unitFrame.ClassificationFrame:SetAlpha(0)
end)

--法术ID
local function ShowID(self,data)
	if not PlateColorDB.showid then return end
	if self:IsTooltipType(Enum.TooltipDataType.Item) then
			self:AddDoubleLine("|cffBA55D3"..ITEMS.."ID:|r|cff00FF00"..data.id.."|r")
	elseif self:IsTooltipType(Enum.TooltipDataType.Unit) then
		local npcid = tonumber(data.guid:match("-(%d+)-%x+$"), 10)
		if npcid and data.guid:match("%a+") ~= "Player" then
			self:AddDoubleLine("|cffBA55D3NPCID:|r|cff00FF00"..npcid.."|r")
		end
	elseif self:IsTooltipType(Enum.TooltipDataType.Spell) and data.id then
		local aura = C_UnitAuras.GetPlayerAuraBySpellID(data.id)
		local source
		if aura and aura.sourceUnit then source = UnitName(aura.sourceUnit) end
		self:AddDoubleLine("|cffBA55D3"..SPELLS.."ID:|r|cff00FF00"..data.id.."|r",source)
	elseif self:IsTooltipType(Enum.TooltipDataType.UnitAura) and data.id then
		local aura = C_UnitAuras.GetPlayerAuraBySpellID(data.id)
		local source
		if aura and aura.sourceUnit then source = UnitName(aura.sourceUnit) end
		self:AddDoubleLine("|cffBA55D3"..AURAS.."ID:|r|cff00FF00"..data.id.."|r",source)
	end
end
TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Unit, ShowID)
TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Item, ShowID)
TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Spell, ShowID)
TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.UnitAura, ShowID)

StaticPopupDialogs.PCNeedPopup = { 
	text = "你开启了|cff00FF00隐藏友方血条|r选项"..
	"\n受暴雪限制,这可能会导致"..
	"\n|cffFFFF00召唤物或友方NPC的血条/能量不显示|r"..
	"\n你可以在|cff00FF00PlateColor|r的|cff00FF00基础|r里"..
	"\n关闭|cff00FF00隐藏友方血条|r选项",
	button1 = OKAY, 
} 
local PCPopupHelp = CreateFrame("Frame")
PCPopupHelp:RegisterEvent("NAME_PLATE_UNIT_ADDED")
PCPopupHelp:SetScript("OnEvent", function(self, event, frame)
	if not PlateColorDB.OnlyName then return end
	if PCPopupHelp.known then return end
	local namePlate = C_NamePlate.GetNamePlateForUnit(frame,false)
	if not namePlate then return end
	local unitFrame = namePlate.UnitFrame
	if not unitFrame.unit then return end
	local guid = UnitGUID(unitFrame.unit)
	local _, _, _, _, _, id = strsplit("-", guid or "") 
	if not ns.needPopupID[id] then return end
	
	StaticPopup_Show("PCNeedPopup")
	PCPopupHelp.known = true
end)